/*
 * ================================================================================变量定义区
 */
var showCount = 10; //定义每页显示条数
var currentPage = 1;//全局变量，当前页

/*
 * ================================================================================页面初始化函数
 */
$(function(){
	//初始化列表，取第一页数据
	getDataList(currentPage);
});


/*
 * ================================================================================函数定义区
 * 
 */

/**
 * 获取列表数据
 * @param currentPage 请求的页码
 */
function getDataList(pageIndex){
	$.ajax({
		type: 'POST',
        url: BASE_PATH +'/test/testUser/listPageData.do',
        dataType:"json",
        data:{
        	"showCount":showCount,
        	"currentPage":pageIndex
        },
        cache:false,
        success:function(data,textStatus){
        	var html = "";        	
        	var pageData = data;
        	var totalResult = pageData.queryParam.totalResult;
        	if(pageData.queryResult != null){
        		//解析data, 组装成html
	        	$.each(pageData.queryResult,function(i,n){
	        		html +='<tr onclick="chooseRow(this,false);"  id="tr_index_'+i+'">'
	        		+'<td title='+n.username+'><input type="hidden" value="'+n.username+'"/>'+n.username+'</td>'
	        		+'<td title='+n.depart_name+'>'+n.depart_name+'</td>'
	        		+'<td title='+n.cn_name+'>'+n.cn_name+'</td>'
	        		+'<td title='+n.emp_code+'>'+n.emp_code+'</td>'
	        		+'<td title='+n.create_user+'>'+n.create_user+'</td>'
	        		+'<td style="text-align:center;" title='+n.create_time+'>'+n.create_time+'</td></tr>';
	        	});  
        		
        	} else{
        		html = '<tr><td colspan="2">No items.</td></tr>';
        	}
        	
        	$('#tBodyList').html(html);
        	currentPage = pageIndex;
        		if(totalResult == 0){
        			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
        		} else {
        			// 创建分页
        			$("#pagination").pagination(totalResult, {
        				num_edge_entries: 1, //边缘页数
        				num_display_entries: 3, //主体页数
        				current_page:currentPage,
        				callback: getDataList,
        				items_per_page:showCount //每页显示1项
        			});
        		};
        },
		error:function(e){
			 alert("加载数据错误！"); return;
		}
	});
} 

/**
 * 查询按钮事件
 */
function queryData(){
	alert(getSelectedRows());
}

/**
 * 新增按钮事件
 */
function newUser(){
	$("#submitForm [name=username]").val('');
	$("#submitForm").submit();
	//window.location.href = BASE_PATH+'/testUser/userDetails.do';
	
}
/**
 * 修改按钮事件
 */
function editUser(){
	var id = getSelectedRows();
	if(!id || id.length<1){
		openAlert('请选择一条记录');
		return false;
	}
	var username = $("#"+id).find("input").val();
	$("#submitForm [name=username]").val(username);
	$("#submitForm").submit();	
}
/**
 * 删除按钮事件
 *  
 */
function deleteUser(){
	var id = getSelectedRows();
	if(!id || id.length<1){
		openAlert('请选择一条记录');
		return false;
	}
	openConfirm("确认删除吗？",null,function(){
		var username = $("#"+id).find("input").val();	
		 
		$.ajax({
			type: 'POST',
	        url: BASE_PATH +'/testUser/deleteUser.do',
	        dataType:"json",       
	        contentType:"application/json;charset=UTF-8",
			data:JSON.stringify(username),
			success : function(data) {
				if(data=='success'){
					openAlert('删除成功',function(){
						getDataList(currentPage);
					});
				}else{
					openAlert('删除失败');
				}
				
			},
			error:function(e){
				openAlert('操作失败！错误信息：<div>'+e.responseText+"</div>");
			}
		});
	});
	
}
function popupUser(){
	var returnValue = popUpPanel(BASE_PATH+"/testUser/popupTest_sitemesh_popup.do","选择用户组",1000,570);
	alert('choosed:'+returnValue);
	
}